package mblog.web.controller.admin;

import mblog.base.utils.FilePathUtils;
import mblog.base.utils.ImageHandleUtils;
import mblog.core.persist.entity.DocInfoPO;
import mblog.core.persist.service.DocInfoService;
import mblog.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin/docInfo")
public class DocInfoController extends BaseController{

    @Autowired
    private DocInfoService docInfoService;

    @RequestMapping("/list")
    public String list(ModelMap model) {
        Map<String,  List<DocInfoPO>> map = docInfoService.list();
        model.put("map", map);
        return "/admin/docInfo/list";
    }

    @RequestMapping(value = "view")
    public String view(@RequestParam(required = false) String id,DocInfoPO infoPO, Model model) {
        DocInfoPO d=null;
        if (id!=null&&!id.equals("0")){
            d= docInfoService.get(Long.valueOf(id));
        }else{
            d=new DocInfoPO();
        }
        model.addAttribute("infoPO", d);
        return "/admin/docInfo/view";
    }

    @RequestMapping("/save")
    public String save(@RequestParam(required = false) String id,DocInfoPO infoPO, String path,
                       Float x, Float y, Float width, Float height, Model model){
        DocInfoPO d=null;
        if (id!=null&&!id.equals("0")){
            d= docInfoService.get(Long.valueOf(id));
        }else{
            d=new DocInfoPO();
        }
        if (width != null && height != null) {
            String root = fileRepoFactory.select().getRoot();
            File temp = new File(root + path);
            File scale = null;

            // 目标目录
            Date nowTime = new Date(System.currentTimeMillis());
            SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String retStrFormatNowDate = sdFormatter.format(nowTime);

            String ava100 = appContext.getAvaDir() + "/doc/menuinfo/"+retStrFormatNowDate+".jpg";
            String dest = root + ava100;
            try {
                // 判断父目录是否存在
                File f = new File(dest);
                if(!f.getParentFile().exists()){
                    f.getParentFile().mkdirs();
                }
                // 在目标目录下生成截图
                String scalePath = f.getParent() + "/" + "1" + ".jpg";
                ImageHandleUtils.truncateImage(temp.getAbsolutePath(), scalePath, x.intValue(), y.intValue(), width.intValue());
                // 对结果图片进行压缩
                ImageHandleUtils.scaleImage(scalePath, dest, 100);
                infoPO.setUrl(ava100);
                scale = new File(scalePath);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                temp.delete();
                if (scale != null) {
                    scale.delete();
                }
            }
        }
        docInfoService.save(infoPO);
        return "redirect:/admin/docInfo/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id,Model model){
        docInfoService.delete(id);
        return "redirect:/admin/docInfo/list";
    }
    public String getAvaPath(long uid, int size) {
        String base = FilePathUtils.getAvatar(uid);
        return String.format("/%s_%d.jpg", base, size);
    }

}